"""
GoodsAddPage:添加新商品----通用信息页面
writer:吕俊
"""
from Common.Base import Base, open_browser
from Page.GoodsListPage import GoodsListPage
from Page.AdminLoginPage import AdminLoginPage, login_url
import random, time


class GoodsAddPage(Base):
    """封装定位器"""
    add_new_goods_loc = ('xpath', '//*[@id="sub-menu-02_goods_add"]/a')
    goods_name_loc = ('xpath', r'//*[@id="general-table"]/tbody/tr[1]/td[2]/input[1]')
    font_loc = ('xpath', r'//*[@id="general-table"]/tbody/tr[1]/td[2]/select')  # 字体样式下拉框定位器
    goods_class_loc = ('xpath', r'//*[@id="general-table"]/tbody/tr[3]/td[2]/select')
    goods_price_loc = ('xpath', r'//*[@id="general-table"]/tbody/tr[7]/td[2]/input[1]')  # 本店价格定位
    confirm_button_loc = ('xpath', '//*[@id="tabbody-div"]/form/div/input[2]')
    reset_button_loc = ('xpath', r'//*[@id="tabbody-div"]/form/div/input[3]')
    goods_id_box_loc = ('xpath', '//*[@id="general-table"]/tbody/tr[2]/td[2]/input')  # 商品货号
    add_goods_class_button_loc = ('xpath', r'//*[@id="general-table"]/tbody/tr[3]/td[2]/button')  # 添加商品类别按钮
    input_class_name_box_loc = ('xpath', '//*[@id="category_add"]/input')  # 类名输入框
    extend_class_loc = ('xpath', '//*[@id="general-table"]/tbody/tr[4]/td[2]/input')  # 扩展分类定位
    extend_content_loc = ('xpath', '//*[@id="general-table"]/tbody/tr[4]/td[2]/select[1]')  # 扩展内容
    set_goods_brand_loc = ('xpath', '//*[@id="general-table"]/tbody/tr[5]/td[2]/select')  # 商品品牌
    supplier_loc = ('xpath', '//*[@id="suppliers_id"]')  # 供货商
    register_user_price_loc = ('xpath', '//*[@id="rank_1"]')  # 注册用户价格
    proxy_user_price_loc = ('xpath', '//*[@id="rank_3"]')  # 代销用户价格
    vip_user_price_loc = ('xpath', '//*[@id="rank_2"]')  # VIP用户价格
    detail_tab_loc = ('xpath', '//*[@id="detail-tab"]')
    text_box_loc = ('xpath', '/html/body')
    goods_list_loc = ('xpath', '//*[@id="sub-menu-01_goods_list"]/a')

    assert_loc = ('xpath', '//*[@id="listDiv"]/table[1]/tbody/tr[3]/td[3]/span')
    """封装定位器"""
    """封装列表"""
    goods = ['家用电器', '    大家电', '        平板电脑', '        冰箱', '        家用空调', '        加电配件',
             '        洗衣机', '数码时尚', '智能硬件', '移动电源', '手机类型', '    小型手机', '    3G手机', '手机',
             '    电池', '耳机', '配件', '    保护壳', '服饰', '充值卡']  # 已有商品种类
    brand = ['飞利浦', '仓品', '夏新']  # 已有商品品牌
    font_list = ['字体样式', '加粗', '下划线', '删除线', '斜体']  # 字体下拉框内容
    """封装列表"""

    def click_add_new_goods_button(self):
        """
        点击添加新商品按钮
        :return:
        """
        self.driver.switch_to.frame('menu-frame')
        self.click(self.add_new_goods_loc)
        self.driver.switch_to.parent_frame()

    def click_goods_list(self):
        """
        从添加商品跳转商品列表
        :return:
        """
        self.driver.switch_to.frame('menu-frame')
        self.click(self.goods_list_loc)
        self.driver.switch_to.parent_frame()

    def add_goods_by_input_required_fields(self, goods_name: str):
        """
        填写必填项封装商品
        :param goods_name: 商品名称
        :param font: 字体样式选择
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.send_keys(self.goods_name_loc, goods_name)
        self.drop_down_list(self.goods_class_loc, random.choice(self.goods))  # 商品分类下拉框
        self.drop_down_list(self.font_loc, random.choice(self.font_list))
        self.send_keys(self.goods_price_loc, text=10)
        self.driver.switch_to.parent_frame()

    def goods_name_box(self, goods_name):
        """
        商品名称输入框
        :param goods_name:要输入的商品名称
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.send_keys(self.goods_name_loc, goods_name)
        self.driver.switch_to.parent_frame()

    def goods_class_drop_down_list(self, goods_class):
        """
        选择类别
        :param goods_class:输入商品类别
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.drop_down_list(self.goods_class_loc, goods_class)
        self.driver.switch_to.parent_frame()

    def goods_price(self, text: int):
        """
        设置本店价格
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.send_keys(self.goods_price_loc, text)
        self.driver.switch_to.parent_frame()

    def font_drop_down(self, font):
        """
        名称字体设置
        :param font:
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.drop_down_list(self.font_loc, font)
        self.driver.switch_to.parent_frame()

    def set_goods_brand(self, brand_name):
        """
        设置商品品牌
        :return:
        """
        if brand_name is False:
            self.driver.switch_to.frame('main-frame')
            self.drop_down_list(self.set_goods_brand_loc, random.choice(self.brand))
            self.driver.switch_to.parent_frame()
        else:
            self.driver.switch_to.frame('main-frame')
            self.drop_down_list(self.set_goods_brand_loc, brand_name)
            self.driver.switch_to.parent_frame()

    def goods_id_box(self, goods_id: str):
        """
        商品货号输入框
        :param goods_id:
        :return:
        """
        iframe = self.driver.find_element_by_xpath('//*[@id="xEditingArea"]/iframe')
        self.driver.switch_to.frame(iframe)
        self.find_element(self.goods_id_box_loc).send_keys(goods_id)
        self.driver.switch_to.parent_frame()

    def add_goods_class_button(self, class_name):
        """
        添加加商品分类按钮
        :return:
        """
        if class_name in self.goods:
            pass
        else:
            self.driver.switch_to.frame('main-frame')
            self.click(self.add_goods_class_button_loc)
            self.send_keys(self.input_class_name_box_loc, class_name)
            self.driver.switch_to.parent_frame()

    def extend_class(self):
        """
        扩展分类按钮
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.click(self.extend_class_loc)
        self.drop_down_list(self.extend_content_loc, random.choice(self.goods))  # 商品分类下拉框
        self.driver.switch_to.parent_frame()

    def set_choice_supplier(self, supplier='不指定供货商属于本店商品'):
        """
        设置供货商
        :param supplier:
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.drop_down_list(self.supplier_loc, supplier)
        self.driver.switch_to.parent_frame()

    def member_price(self, num1=-1, num2=-1, num3=-1):
        """
        设置会员价格
        :param num1:
        :param num2:
        :param num3:
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.send_keys(self.register_user_price_loc, num1)
        self.send_keys(self.proxy_user_price_loc, num2)
        self.send_keys(self.vip_user_price_loc, num3)
        self.driver.switch_to.parent_frame()

    def input_detail_text(self, text: str):
        """
        输入详细描述内容
        :param text:
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.click(self.detail_tab_loc)
        self.send_keys(self.text_box_loc, text)
        self.driver.switch_to.parent_frame()

    def confirm_button(self):
        """
        点击确定按钮
        :return:点击完成
        """
        self.driver.switch_to.frame('main-frame')
        self.click(self.confirm_button_loc)
        self.driver.switch_to.parent_frame()

    def reset_button(self):
        """
        点击重置按钮
        :return: 重置完成
        """
        self.driver.switch_to.frame('main-frame')
        self.click(self.reset_button_loc)
        self.driver.switch_to.parent_frame()

    def assert_condition_by_alert(self):
        """
        通过弹窗断言条件
        :return:
        """
        time.sleep(3)
        text = self.gain_switch_alert()
        return text

    def assert_condition_text(self):
        """
        通过文本值断言
        :return:
        """
        self.driver.switch_to.frame('main-frame')
        self.get_element_text(self.assert_loc)
        self.driver.switch_to.parent_frame()


if __name__ == '__main__':
    driver = open_browser()
    admin_login = AdminLoginPage(driver)
    admin_login.go_url(login_url)
    select_goods = GoodsListPage(driver)
    select_goods.click_goods_admin_button()
    add_goods = GoodsAddPage(driver)
    add_goods.click_add_new_goods_button()

    # add_goods.add_goods_by_input_required_fields('大衣')
    # add_goods.reset_button()
    # time.sleep(2)
    # add_goods.add_goods_by_input_required_fields('红米')
    # time.sleep(2)
    # goods_list = GoodsListPage(driver)
    # goods_list.click_goods_admin_button()
    # add_goods.click_goods_list()
    time.sleep(2)
    # goods_list.select_goods_by_keyword('红米')
    # goods_list.check_button()
    # text = goods_list.assert_select()
    add_goods.confirm_button()

    time.sleep(5)
    text = driver.switch_to.alert.text
    print(text)
    # add_goods.confirm_button()

    admin_login.close_browser()
